package com.jiang.专题.矩阵.Q54;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/22 15:33
 */
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        if (matrix.length == 0) return new ArrayList<Integer>();
        int l = 0, r = matrix[0].length - 1, u = 0, d = matrix.length - 1;
        ArrayList<Integer> list = new ArrayList<>();
        while(true) {
            for(int i = l; i <= r; i++) {
                list.add(matrix[u][i]);
            }
            if (u == d) break;
            u++;
            for(int i = u; i <= d; i++) {
                list.add(matrix[i][r]);
            }
            if (l == r) break;
            r--;
            for(int i = r; i >= l; i--) {
                list.add(matrix[d][i]);
            }
            if (u == d) break;
            d--;
            for(int i = d; i >= u; i--) {
                list.add(matrix[i][l]);
            }
            if (l == r) break;
            l++;
        }
        return list;
    }


}
